package src.ee.ioc.smaug.fsm;

public class FiniteAcceptor extends FiniteStateMachine{
    /* (non-Javadoc)
     * @see src.ee.ioc.smaug.fsm.FiniteStateMachine#makeTransition(char)
     */
    public void makeTransition(char inputChar) {
        Transition tr = getTransition(transitionTable[currentState.getStateID()][this
                .getInputAlphabet().findPosition(inputChar)]);
        if (tr == null) {
            this.setRunning(false);
            System.err.println("Machine stopped, transition not found");
            setOutString("not accepted");
            return;
        } else {
            currentState = tr.getToState();
           
        }
    }

    /* (non-Javadoc)
     * @see src.ee.ioc.smaug.fsm.FiniteStateMachine#runMachine()
     */
    public void runMachine() {
        currentState =  states.get(0);
        System.out.println("Initial state " + currentState);

        for (int i = 0; i < inString.length(); i++) {
            if (!this.isRunning()) {
                System.err.println("End of work");
                return;
            } else {
                makeTransition(inString.charAt(i));
            }
        }
        if (currentState.isFinalState()){
            setOutString("accepted");
        }
        else {
            setOutString("not accepted");
        }
    }
   
}
